package com.zsxb.crm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zsxb.crm.dto.RoleDTO;
import com.zsxb.crm.dto.RolePageDTO;
import com.zsxb.crm.mapper.RoleMapper;
import com.zsxb.crm.pojo.Role;
import com.zsxb.crm.result.PageResult;
import com.zsxb.crm.service.RoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 2024/10/28
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class RoleServiceImpl implements RoleService {

    private final RoleMapper roleMapper;

    /**
     * 角色分页查询
     *
     * @param rolePageDTO
     * @return
     */
    @Override
    public PageResult rolePage(RolePageDTO rolePageDTO) {
        PageHelper.startPage(rolePageDTO.getPageNum(), rolePageDTO.getPageSize());

        //解析数据
        if (rolePageDTO.getParams() != null) {
            String beginTime = rolePageDTO.getParams().get("beginTime").toString();
            rolePageDTO.setBeginTime(beginTime);
            String endTime = rolePageDTO.getParams().get("endTime").toString();
            rolePageDTO.setEndTime(endTime);
        }
        log.info("角色分页查询{}", rolePageDTO);
        Page<Role> roleList = (Page<Role>) roleMapper.rolePage(rolePageDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(roleList.getTotal());
        pageResult.setRows(roleList.getResult());
        return pageResult;
    }

    /**
     * 根据id查询角色
     *
     * @param id
     * @return
     */
    @Override
    public Role selectById(Integer id) {
        Role role = roleMapper.selectById(id);
        return role;
    }

    /**
     * 删除角色
     *
     * @param ids
     */
    @Override
    public void delete(String ids) {
        String[] split = ids.split(",");
        roleMapper.delByIds(split);
    }


    /**
     * 修改角色
     *
     * @param role
     */
    @Override
    public void update(Role role) {
        log.info("修改角色信息:{}", role);
        roleMapper.update(role);
    }

    /**
     * 修改角色数据权限
     * @param dto
     */
    @Override
    public void updateDataScope(RoleDTO dto) {
        log.info("修改角色数据权限:{}", dto);
        //roleMapper.updateDataScope(dto);
        return;
    }

}
